package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.RoleUserDOMapper;
import com.livepan.server.dataobject.RoleUserDO;
import com.livepan.server.dataobject.RoleUserDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.RoleUserModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleUserService {
    @Autowired
    private RoleUserDOMapper roleUserDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public List<RoleUserModel> list(String userId) {
        RoleUserDOExample roleUserDOExample = new RoleUserDOExample();
        RoleUserDOExample.Criteria criteria = roleUserDOExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        roleUserDOExample.setOrderByClause("id asc");
        List<RoleUserDO> roleUserDOList = roleUserDOMapper.selectByExample(roleUserDOExample);
        List<RoleUserModel> roleUserModelList = roleUserDOList.stream().map(
                this::convertRoleUserModelFromRoleUserDO
        ).collect(Collectors.toList());
        return roleUserModelList;
    }

    public void save(String userId, List<String> roleIdList) throws BusinessException {
        ValidationResult validateResult = validator.validate(roleIdList);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        for (String roleId : roleIdList) {
            RoleUserDO roleUserDO=new RoleUserDO();
            roleUserDO.setRoleId(roleId);
            roleUserDO.setUserId(userId);
            insert(roleUserDO);
        }
    }

    private void insert(RoleUserDO roleUserDO) {
        roleUserDO.setId(UUIDUtil.getShortUUID());
        roleUserDOMapper.insert(roleUserDO);
    }


    public void delete(String id) {
        roleUserDOMapper.deleteByPrimaryKey(id);
    }

    public void clear(String userId) {
        RoleUserDOExample roleUserDOExample = new RoleUserDOExample();
        RoleUserDOExample.Criteria criteria = roleUserDOExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        roleUserDOMapper.deleteByExample(roleUserDOExample);

    }


    private RoleUserModel convertRoleUserModelFromRoleUserDO(RoleUserDO roleUserDO) {
        if (roleUserDO == null) {
            return null;
        }
        RoleUserModel roleUserModel = new RoleUserModel();
        BeanUtils.copyProperties(roleUserDO, roleUserModel);
        return roleUserModel;
    }
}
